package com.zhudao.crawler.util;

import java.security.SecureRandom;
import javax.crypto.Cipher;  
import javax.crypto.SecretKey;  
import javax.crypto.SecretKeyFactory;  
import javax.crypto.spec.DESKeySpec;  
  
public class Decode458 {  
    static byte[] key = null; //这个key如果有需要请反编译58客户端获取这里不便贴出  
      
    public static void main(String[] args) throws Exception {  
        System.out.println(new String(Decode458.decode(Decode458.convertHexString("002E674657AE8239982087DCB2E6A99B"))));  
        System.out.println(Decode458.toHexString(Decode458.encode("13219863008".getBytes())));  
    }  
      
    public static byte[] decode(byte[] paramArrayOfByte) {  
        try {  
            SecureRandom localSecureRandom = new SecureRandom();  
            DESKeySpec localDESKeySpec = new DESKeySpec(key);  
            SecretKey localSecretKey = SecretKeyFactory.getInstance("DES")  
                    .generateSecret(localDESKeySpec);  
            Cipher localCipher = Cipher.getInstance("DES");  
            localCipher.init(2, localSecretKey, localSecureRandom);  
            return localCipher.doFinal(paramArrayOfByte);  
        } catch (Exception e) {  
            e.printStackTrace();  
            return null;  
        }  
    }  
      
    public static byte[] encode(byte[] paramArrayOfByte) {  
        try {  
            SecureRandom localSecureRandom = new SecureRandom();  
            DESKeySpec localDESKeySpec = new DESKeySpec(key);  
            SecretKey localSecretKey = SecretKeyFactory.getInstance("DES")  
                    .generateSecret(localDESKeySpec);  
            Cipher localCipher = Cipher.getInstance("DES");  
            localCipher.init(1, localSecretKey, localSecureRandom);  
            return localCipher.doFinal(paramArrayOfByte);  
        } catch (Exception e) {  
            e.printStackTrace();  
            return null;  
        }  
    }  
      
    public static byte[] convertHexString(String text) {  
        byte digest[] = new byte[text.length() / 2];  
        for (int i = 0; i < digest.length; i++) {  
            String byteString = text.substring(2 * i, 2 * i + 2);  
            int byteValue = Integer.parseInt(byteString, 16);  
            digest[i] = (byte) byteValue;  
        }  
        return digest;  
    }  
      
    public static String toHexString(byte b[]) {  
        StringBuffer hexString = new StringBuffer();  
        for (int i = 0; i < b.length; i++) {  
            String plainText = Integer.toHexString(0xff & b[i]);  
            if (plainText.length() < 2)  
                plainText = "0" + plainText;  
            hexString.append(plainText);  
        }  
        return hexString.toString();  
    }  
}  